package com.htrq.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.htrq.entity.CNGDatachk;
import com.htrq.entity.Page;
import com.htrq.service.CNGCHKService;
import com.htrq.util.ExportToExcelUtil;

@Controller
@RequestMapping("jsp/cng")
public class CNGCHKController {
	@Resource
	private CNGCHKService chService;

	@RequestMapping("getAllchk")
	public String getAllchk(CNGDatachk chk, Page page,
			HttpServletResponse response) throws IOException {
		List<CNGDatachk> list = chService.getAllchk(chk, page);
		String str = JSON.toJSONString(list);
		Long total = chService.getTotal(chk);
		String json = "{total:" + total + ",chks:" + str + "}";
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		out.write(json);
		return null;
	}

	@RequestMapping("saveChk")
	public String saveChk(CNGDatachk chk, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		boolean flag = chService.saveChk(request, chk);
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		if (flag) {
			out.print("{'success':true,'res':{'result':'保存成功,请点刷新查看'}}");
		} else {
			out.print("{'success':true,'res':{'result':'保存失败,请查看数据'}}");
		}
		return null;
	}

	@RequestMapping("updateChk")
	public String updateChk(CNGDatachk chk, HttpServletResponse response)
			throws IOException {
		boolean flag = chService.updateChk(chk);
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		if (flag) {
			out.print("{'success':true,'res':{'result':'修改成功,请点刷新查看'}}");
		} else {
			out.print("{'success':true,'res':{'result':'修改失败'}}");
		}
		return null;
	}

	@RequestMapping("getChkById")
	@ResponseBody
	public String getChkById(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ParseException {
		// List<TResource> rights = .getRightByUser(user);
		CNGDatachk chk = chService.getChkById(request.getParameter("rid"));
		String json = JSON.toJSONString(chk);
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		out.print(json);
		return null;
	}

	@RequestMapping("getChkforJson")
	public String getChkforJson(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ParseException {
		String rid = request.getParameter("rids");
		CNGDatachk chk = chService.getChkById(rid);
		String str = JSON.toJSONString(chk);
		String json = "{total:" + 1 + ",chks:" + str + "}";
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		out.write(json);
		return null;
	}

	@RequestMapping("deleteById")
	public String deleteById(CNGDatachk chk, HttpServletResponse response)
			throws IOException {
		boolean flag = chService.del(chk);
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		if (flag) {
			out.print("{'success':true,'res':{'result':'删除成功'}}");
		} else {
			out.print("{'success':true,'res':{'result':'修改失败'}}");
		}
		return null;
	}

	@RequestMapping("exportchk")
	public String exportchk(CNGDatachk chk, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String titles[] = { "单据号", "出库日期", "出库公司", "发货单位", "收货单位", "送气量",
				"出库类型", "承运方式", "车牌号", "挂号", "操作工", "司机", "押运员", "备注" };
		List<Object[]> list = chService.exportbyForm(chk);
		ExportToExcelUtil.exportToExcel(request, response, "CNG出库单查询导出结果.xls",
				"磅单", titles, list);
		return null;
	}
}
